package net.ustc.javamemcached.protocol;

import java.util.List;

import net.ustc.javamemcached.protocol.Protocol.Parameter;
import net.ustc.javamemcached.protocol.Protocol.ParameterType;

import org.jboss.netty.buffer.ChannelBuffer;

public class ProtocolMessage {
	private ChannelBuffer command;
	private List<ChannelBuffer> parameters;
	private ChannelBuffer data;
	
	private Protocol protocol;
	
	public ProtocolMessage(ChannelBuffer command, List<ChannelBuffer> parameters, ChannelBuffer data, Protocol protocol){
		this.command = command;
		this.parameters =parameters;
		this.data = data;
		this.protocol = protocol;
	}
	
	public Object getPrameterValue(String parameterName){
		Parameter parameter = protocol.getParameters().get(parameterName);
		ParameterType parameterType = parameter.getParameterType();
		int index = parameter.getIndex();
		switch(parameterType){
		case INT:return parameters.get(index).readInt();
		case LONG:return parameters.get(index).readLong();
		case STRING:return parameters.get(index).toString(Protocol.USASCII);
		default:return parameters.get(index).toString(Protocol.USASCII);
		}
	}

	public ChannelBuffer getData() {
		return data;
	}

	public ChannelBuffer getCommand() {
		return command;
	}

	public List<ChannelBuffer> getParameters() {
		return parameters;
	}

	public Protocol getProtocol() {
		return protocol;
	}
}


